package com.flower.salary.service.impl;

import cn.hutool.core.date.DateTime;
import com.flower.salary.common.utils.SalaryException;
import com.flower.salary.service.FileUpload;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * @author lavender
 * @version 1.0
 * @date 2023-12-31 16:41
 */
@Service
public class FileUploadImpl implements FileUpload {

    //路径
    private static final String UPLOAD_DIR = "E:\\img\\";
    @Override
    public String upload(MultipartFile file)  {
        try {
            // 将文件内容转化为字节数组
            byte[] bytes = file.getBytes();
            String fileName = file.getOriginalFilename();
            String[] split = fileName.split("\\.");

            // 新路径
            UUID u = UUID.randomUUID();
            // 给文件重命名
            String filePath = UPLOAD_DIR + u+ "." + split[1];
            File dest = new File(filePath);

            FileOutputStream out = new FileOutputStream(dest);
            out.write(bytes);
            out.close();

            return "http://127.0.0.1:8080/img/" + u+ "." + split[1];

        } catch (IOException e) {
            throw new SalaryException("上传头像失败");
        }


    }

    public static void main(String[] args) {
        LocalDateTime date = LocalDateTime.now();
        int year = date.getYear();
        System.out.println(year);
    }


}
